package com.ruoyi.common.core.basic;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.exceptions.TooManyResultsException;

import java.util.Collection;
import java.util.List;

/**
 * @author : hcl
 * @Date : 2023/8/10 15:10
 */
public interface BasicMapper<T> extends BaseMapper<T> {
    Integer insertBatchSomeColumn(Collection<T> list);

    List<Long> findIds(@Param("ew") Wrapper<T> queryWrapper);

    List<Long> selectLongs(@Param("ew") Wrapper<T> queryWrapper);

    default Long findId(@Param("ew") Wrapper<T> queryWrapper) {
        List<Long> list = this.findIds(queryWrapper);
        // 抄自 DefaultSqlSession#selectOne
        if (list.size() == 1) {
            return list.get(0);
        } else if (list.size() > 1) {
            throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
        } else {
            return null;
        }
    }
}
